package com.khh.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * Created by webrx on 2017-09-05.
 */

//入门测试springmvc

@Controller
public class TestController {

    @RequestMapping("/test1") @ResponseBody
    public void hello(){

        System.out.println("fhkdfhuueih");
    }

    @RequestMapping("/u") @ResponseBody
    public void u(HttpServletRequest req, HttpServletResponse res) throws IOException {

        PrintWriter out = res.getWriter();
        out.print("hello中国人");
        out.flush();
        out.close();
    }


    @RequestMapping("/info")
    public ModelAndView info(ModelAndView mv){
        mv.setViewName("test/info"); //根目录下info.jsp为视图文件
        mv.addObject("name","张三丰");
        mv.addObject("address","河南省郑州市文化路28号");
        return mv;
    }

    @RequestMapping("/ok")
    public String show(Model m){
        m.addAttribute("name","李四四");
        return "test/ok"; // 根目录下的 ok.jsp 视图文件
    }



    //参数传递 http://localhost:8080/del?name=xiaoli
    @RequestMapping("/del")
    public ModelAndView del(HttpServletRequest req) throws UnsupportedEncodingException {
        ModelAndView m = new ModelAndView("test/success");  //渲染视图为 success.jsp
        m.addObject("user",req.getParameter("name"));
        //m.setViewName("aa");//aa.jsp
        //m.setViewName("cc"); //cc.jsp
        //m.setViewName("ddd"); //ddd.jsp
        return m;
    }

    @RequestMapping(value="myshow")
    public String shows(Model m){
        m.addAttribute("msg","显示数据1222412412431234124312");  //此参数可以在显示层显示数据
        return "test/show";  // 会渲染show.jsp 视图层
    }

    //post 请求 可以接值
    @RequestMapping(value = "sss",method= RequestMethod.POST) @ResponseBody
    public void save(HttpServletRequest req) throws UnsupportedEncodingException{
        req.setCharacterEncoding("utf-8");
        System.out.println(req.getParameter("name"));
        System.out.println("POST");
    }

    //springmvc restful风格方式
    @RequestMapping(value="save/{id}")
    public void save(@PathVariable("id") Integer id, HttpServletResponse res) throws IOException{

        PrintWriter out = res.getWriter();
        out.print(id);
        out.flush();
        out.close();
    }

    @RequestMapping(value="saves/{id}/{name}")
    public void saves(@PathVariable("name") String name,@PathVariable("id") Integer id, HttpServletResponse res) throws IOException{

        PrintWriter out = res.getWriter();
        out.print(id+":"+name);
        out.flush();
        out.close();
    }


    //@RequestMapping(value = "uuuu")
    @RequestMapping("/uuuu")
    public String user(HttpServletRequest req){
        req.setAttribute("user","lisi李四四");
        //return "test/success"; //显示  /user.jsp视图   派发转发
        return "redirect: index.jsp";  //重定向跳转 不能直接跳转到WEB-INF目录下
    }

    @RequestMapping("/u3")
    public ModelAndView user(){
        //return new ModelAndView("redirect:/user.jsp");  //重定向跳转


        //派发转发，可以传递值
        ModelAndView mv = new ModelAndView("user");// /user.jsp 视图渲染
        mv.addObject("user","张三丰11111"); //user.jsp ${user} 显示
        //mv.setViewName("ok"); //ok.jsp
        //mv.setViewName("abc");//abc.jsp
        return mv;
    }


}
